//
//  HMHomeViewController.swift
//  HMWeibo1
//
//  Created by liGordon on 16/4/9.
//  Copyright © 2016年 lifuli. All rights reserved.
//

import UIKit

import SVProgressHUD

 /// 导航栏显示什么内容不是看导航控制器  看导航控制器当前正在显示的子控制器

class HMHomeViewController: HMBaseTableViewController {

    // 微博模型
    var statuses: [HMStatus]?
    
    private let ReuseIdentifier = "ReuseIdentifier"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
      
        
        // 判断用户已经登录,才需要做下面的操作
        if !HMUserAccountViewModel.sharedInstance.userLogin{
            HMPrint("没有登录不做操作")
            return
        }
        
        
        setupNavigationBar()
        
        
        
        
        
        //1. cell的高度
        tableView.rowHeight = UITableViewAutomaticDimension
        
        //
        tableView.estimatedRowHeight = 200
        
        
        
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        
        
        // 注册cell
        tableView.registerClass(HMStatusCell.self, forCellReuseIdentifier: ReuseIdentifier)
        
    
        
        // 测试加载微博数据
        HMStatusViewModel.shareInstance.loadStatus { (statuse, error) -> () in
            if error != nil {
                HMPrint("获取微博数据失败: \(error)")
                
                SVProgressHUD.showErrorWithStatus("您的网络不给力...")
                return
            }
            
            // 加载微博成功,将微博数据保存起来,让tableView去显示
            self.statuses =  statuse
           
            self.tableView.reloadData()
            
            // HMPrint("self.statuses: \(self.statuses)")
        }

    }
    
    
    private func setupNavigationBar() {
//        // 左边

      //  navigationItem.leftBarButtonItem = createBarButtonItem("navigationbar_friendsearch")
//        let leftButton = UIButton()
//        leftButton.setImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
//        leftButton.setImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
//         navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)
//    
//        
//        let rightButton = UIButton()
//        rightButton.setImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
//        rightButton.setImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
//        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: Selector("didClickPerson:"))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "didClickQRCode")
        
        
        // title
        let titleButton = HMHomeTitleButton(title:"我就看看不说话")
        titleButton.addTarget(self, action: Selector("didClickTitleButton:"), forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = titleButton
        
    }
    
    //Mark - 按钮点击事件
    @objc private func didClickTitleButton(button: UIButton) {
        // 切换按钮的选中状态
        button.selected = !button.selected
        
        UIView.animateWithDuration(HMDefaultAnimDuration) { () -> Void in
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.0001)) : CGAffineTransformIdentity
        }
//        HMPrint("点击了")
    }
    
    
    
    @objc private func didClickPerson(button: UIButton) {
        HMPrint("button: \(button)")
    }
    
    
    @objc private func didClickQRCode(button: UIButton) {
        HMPrint("button: \(button)")
    }

    
  
}

    // Mark: - 扩展 HMHomeTableViewController 将tableView的数据源方法放到这里
extension HMHomeViewController {
    
    
    
    
    // 返回cell的数
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statuses?.count ?? 0
    }
    
    // 返回每个cell的内容
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! HMStatusCell
        
        // cell显示之前.将模型传递给cell
        cell.status = self.statuses![indexPath.row]
        
        return cell
    }
    
    
    /// cell不要高亮
    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return false
    }
}